﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
    public class adminDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();

        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int adminId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from admin where adminId=@adminId ");
            htSyn.Add("adminId", adminId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string adminUser)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from admin where adminUser=@adminUser ");
            htSyn.Add("adminUser", adminUser);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得管理员列表
        /// <summary>
        /// 获得管理员列表
        /// </summary>
        /// <returns></returns>
        public IList<admin> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("SELECT [adminId],[adminUser],[adminPwd],[adminName],[adminSex],[adminTel],[adminQQ],[adminEmail],[adminMark],[adminState],[adminDate] FROM [admin]");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据管理员号查询实体
        /// <summary>
        /// 根据管理员号查询实体
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns></returns>
        public admin GetModel(int adminId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select adminId,adminUser,adminPwd,adminName,adminSex,adminTel,adminQQ,adminEmail,adminMark,adminState,adminDate from admin ");
            strSql.Append(" where adminId=@adminId ");
            htSyn.Add("@adminId", adminId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            admin admin = new admin();
            if (dt.Rows.Count>0)
            {
                admin.adminId = Convert.ToInt32(dt.Rows[0]["adminId"]);
                admin.adminUser = dt.Rows[0]["adminUser"].ToString();
                admin.adminPwd = dt.Rows[0]["adminPwd"].ToString();
                admin.adminName = dt.Rows[0]["adminName"].ToString();
                admin.adminSex = dt.Rows[0]["adminSex"].ToString();
                admin.adminTel = dt.Rows[0]["adminTel"].ToString();
                admin.adminQQ = dt.Rows[0]["adminQQ"].ToString();
                admin.adminEmail = dt.Rows[0]["adminEmail"].ToString();
                admin.adminMark = dt.Rows[0]["adminMark"].ToString();
                admin.adminState = Convert.ToBoolean(dt.Rows[0]["adminState"]);
                admin.adminDate = Convert.ToDateTime(dt.Rows[0]["adminDate"]);
            }
            return admin;
        }
        #endregion

        #region 修改管理员信息
        /// <summary>
        /// 修改管理员信息
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public bool Update(admin admin)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update admin set ");
            strSql.Append("adminUser=@adminUser,");
            strSql.Append("adminPwd=@adminPwd,");
            strSql.Append("adminName=@adminName,");
            strSql.Append("adminSex=@adminSex,");
            strSql.Append("adminTel=@adminTel,");
            strSql.Append("adminQQ=@adminQQ,");
            strSql.Append("adminEmail=@adminEmail,");
            strSql.Append("adminMark=@adminMark,");
            strSql.Append("adminState=@adminState,");
            strSql.Append("adminDate=@adminDate");
            strSql.Append(" where adminId=@adminId ");
            htSyn.Add("@adminUser", admin.adminUser);
            htSyn.Add("@adminPwd", admin.adminPwd);
            htSyn.Add("@adminName", admin.adminName);
            htSyn.Add("@adminSex", admin.adminSex);
            htSyn.Add("@adminTel", admin.adminTel);
            htSyn.Add("@adminQQ", admin.adminQQ);
            htSyn.Add("@adminEmail", admin.adminEmail);
            htSyn.Add("@adminMark", admin.adminMark);
            htSyn.Add("@adminState", admin.adminState);
            htSyn.Add("@adminDate", admin.adminDate);
            htSyn.Add("@adminId", admin.adminId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除管理员根据管理员号
        /// <summary>
        /// 删除管理员根据管理员号
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns></returns>
        public bool Delete(int adminId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from admin where adminId=@adminId");
            htSyn.Add("adminId", adminId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加管理员
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public bool Insert(admin admin)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into admin([adminUser],[adminPwd],[adminName],[adminSex],[adminTel],[adminQQ],[adminEmail],[adminMark],[adminState],[adminDate]) values (@adminUser,@adminPwd,@adminName,@adminSex,@adminTel,@adminQQ,@adminEmail,@adminMark,@adminState,@adminDate)");
            htSyn.Add("@adminUser", admin.adminUser);
            htSyn.Add("@adminPwd", admin.adminPwd);
            htSyn.Add("@adminName", admin.adminName);
            htSyn.Add("@adminSex", admin.adminSex);
            htSyn.Add("@adminTel", admin.adminTel);
            htSyn.Add("@adminQQ", admin.adminQQ);
            htSyn.Add("@adminEmail", admin.adminEmail);
            htSyn.Add("@adminMark", admin.adminMark);
            htSyn.Add("@adminState", admin.adminState);
            htSyn.Add("@adminDate", admin.adminDate);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<admin> getProductsBySql(string strSql)
        {
            IList<admin> list = new List<admin>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    admin admin = new admin();
                    admin.adminId = Convert.ToInt32(dr["adminId"]);
                    admin.adminUser = dr["adminUser"].ToString();
                    admin.adminPwd = dr["adminPwd"].ToString();
                    admin.adminName = dr["adminName"].ToString();
                    admin.adminSex = dr["adminSex"].ToString();
                    admin.adminTel = dr["adminTel"].ToString();
                    admin.adminQQ = dr["adminQQ"].ToString();
                    admin.adminEmail = dr["adminEmail"].ToString();
                    admin.adminMark = dr["adminMark"].ToString();
                    admin.adminState = Convert.ToBoolean(dr["adminState"]);
                    admin.adminDate = Convert.ToDateTime(dr["adminDate"]);
                    list.Add(admin);
                }
            }
            return list;
        }
        #endregion
    }
}
